// pages/manager/manager.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //保存有预约记录的日期数组
    activeNames: [],
    //当前手风琴
    current:'',
    //保存指定某日预约记录数据数组
    signUps:[],
    //是否完成请求
    loading:true
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //调用云函数 -> 获取有预约记录的日期
    wx.cloud.callFunction({
      name: 'selectByDate',
      success: res => {
        this.setData({
          activeNames: res.result.list
        })
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      loading:true
    })
    
    var current = that.data.current;
    if(current.length > 0){
      //重新请求预约记录
      wx.cloud.callFunction({
        name: 'selectByDate',
        data:{
          date:current
        },
        success: res => {
          that.setData({
            signUps:res.result.data,
          })
          setTimeout(() => {
            that.setData({
              loading:false
            })
          }, 200);
        }
      });
    }else{
      //重新请求预约时间数组
      wx.cloud.callFunction({
        name: 'selectByDate',
        success: res => {
          this.setData({
            activeNames: res.result.list
          })
        }
      });
    }
  },

  //切换
  onChange(event) {
    var that = this;
    var current = event.detail;
    that.setData({
      current:current,
      loading:true
    })
    wx.cloud.callFunction({
      name: 'selectByDate',
      data:{
        date:current
      },
      success: res => {
        that.setData({
          signUps:res.result.data,
        })
        setTimeout(() => {
          that.setData({
            loading:false
          })
        }, 200);
      }
    });
  },

  //删除预约记录 -> 删除数据库记录 -> 重新请求预约记录列表
  deleteLog(event){
    var that = this;
    var id = event.target.dataset.id;
    //设置加载中的状态
    that.setData({
      loading:true
    })
    //请求删除数据库操作
    wx.cloud.callFunction({
      name: 'deleteById',
      data:{
        id:id
      },
      success: res => {
        //请求该日期下剩余的预约记录
        var current = that.data.current;
        wx.cloud.callFunction({
          name: 'selectByDate',
          data:{
            date:current
          },
          success: res => {
            var signUps = res.result.data;
            //如果还有剩余的预约记录则直接显示
            if(signUps.length > 0){
              that.setData({
                signUps:signUps,
              })
              setTimeout(() => {
                that.setData({
                  loading:false
                })
              }, 200);
            }else{
              //没有预约记录则重新请求有预约记录的日期
              wx.cloud.callFunction({
                name: 'selectByDate',
                success: res => {
                  that.setData({
                    activeNames: res.result.list,
                    current:''
                  })
                }
              });
            }
          }
        });
      }
    });
  },


  //拨打电话
  callPhone(event){
    var phone = event.target.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  }
})